package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;

import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.WeightData;
import com.zbw.weightlibrary.Util.ThreadPoolManager;
import com.zqebsdk.zqebsdk;
import com.zqprintersdk.PrinterConst;

/**
 * 作者 ：Wesley
 * 时间 ：2022-09-22 11:04
 * ZhongQiSerialPort 说明：
 * 中琦串口秤
 */
public class ZhongQiSerialPort implements IBleControl {
    private final IinfoCallback mCallback;
    private final String mPort; //串口电子称端口port
    private final int mBaudrate; //串口电子称波特率baudrate
    private zqebsdk zqeb;
    private Context mContext;
    private boolean isStart;
    private boolean isConnect;

    public ZhongQiSerialPort(Context context, IinfoCallback callback, WeightData data) {
        mContext = context;
        mCallback = callback;
        mPort = data.getPort();
        mBaudrate = data.getBaudrate();
        zqeb = new zqebsdk();
    }

    @Override
    public void bleConnect() {
        try {
            int isConnection = zqeb.EB_Connect(mPort+":"+mBaudrate,mContext);
            if(isConnection == PrinterConst.ErrorCode.SUCCESS){
                isConnect = true;
                mCallback.onConnect("连接成功",true);
            } else {
                mCallback.onConnect("连接失败",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            mCallback.onConnect("连接失败",false);
        }
    }

    @Override
    public void bleDisconnect() {
        try {
            isConnect = false;
            zqeb.EB_Disconnect();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            mCallback.onDisconnect("断开成功",true);
        }
    }

    @Override
    public void getWeight() {
        isStart = true;
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                while(isStart) {
                    if (isConnect) {
                        ReadData();
                    }
                    try {
                        Thread.sleep(80);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    /**
     * 读数据
     */
    private void ReadData() {
        try {
            String[] strRet = zqeb.EB_GetWeightTare();
            if(strRet != null && strRet.length == 3){
                boolean State = "0".equals(strRet[0]);
                String strWeight = strRet[1];
                String strTare = strRet[2];
                strWeight = strWeight.replace("kg","");

                mCallback.onGetWeight(strWeight, State);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @Override
    public void stopWeight() {
        isStart = false;
    }

    @Override
    public void clearTare() {
        try {
            zqeb.EB_ClearTare();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void tare() {
        try {
            zqeb.EB_SetNetWeight();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void zero() {
        try {
            zqeb.EB_SetZero();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
